
#include "mainwindow.h"
#include "ui_mainwindow.h"




void MainWindow::CreateDB()
{
    QSqlDatabase DB;
    DB=QSqlDatabase::addDatabase("QSQLITE");
    DB.setDatabaseName("legacies.db");
    DB.open();
}

void MainWindow::DeleDB()
{
    QSqlDatabase DB;
    DB=QSqlDatabase::addDatabase("QSQLITE");
    DB.setDatabaseName("legacies.db");
    DB.close();

}

//初始化已有记录
void MainWindow::ReadId()
{

    QSqlQuery Query;
    QString Select("select 标识码 from List");
    Query.exec(Select);
    while(Query.next())
        IdList.append(Query.value(0).toString());

}

//设置列表当前行
void MainWindow::ListInit(int num)
{
    this->InfoList->setCurrentRow(num);
    //this->InfoList->setCurrentRow(num);
}







//刷新记录函数
void MainWindow::RenewList(int num)
{

    QSqlQuery Query;
    this->InfoList->clear();
//    this->InfoList->clear();
    for(int i=0;i<IdList.size();i++)
    {

        QString Select=QString("select 圣遗物类型, 圣遗物主词条, 等级 from List where 标识码 = '%1'").arg(IdList.at(i));
        Query.exec(Select);
        Query.next();
        QString Output=Query.value(0).toString()+" "+Query.value(1).toString()+" "+QString::number(Query.value(2).toUInt())+"级";
        this->InfoList->addItem(Output);
        //this->InfoList->addItem(Output);
        ListInit(num);
    }



}
//窗口构造函数
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{

    ui->setupUi(this);

    DelBttn= new DeleButton();
    DelBttn->setParent(this);
    DelBttn->setGeometry(240, 200, 100, 100);
    //  DelBttn->show();
    connect(this->DelBttn,SIGNAL(clicked()),this,SLOT(DeleWithIDList()));

    GenBttn = new GenButton();
    GenBttn->setParent(this);
    GenBttn->setGeometry(240, 300, 100, 100);
    connect(this->GenBttn,SIGNAL(clicked()),this,SLOT(GenWithIDList()));

    RefBttn = new QPushButton;
    RefBttn->setParent(this);
    RefBttn->setGeometry(340, 200, 100, 100);
    RefBttn->setStyleSheet("QPushButton{font:18px;}");
    RefBttn->setText("恢复旧数据");
    connect(this->RefBttn,SIGNAL(clicked()),this,SLOT(RenewListSlot()));

    UpGBttn = new UpGButton();
    UpGBttn->setParent(this);
    UpGBttn->setGeometry(340, 300, 100, 100);
    connect(this->UpGBttn,SIGNAL(clicked()),this,SLOT(UpGWithIDList()));

    InfoList = new QListWidget;
    InfoList->setParent(this);
    InfoList->setGeometry(2, 2, 230,400);
//    InfoList->setStyleSheet("QListWidgetItem{font:20px;}");

    InfoList->setFont(QFont("SimHei", 13));
    connect(this->InfoList,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(ListItemClicked()));

    InfoLabel = new QLabel;
    InfoLabel->setParent(this);
    InfoLabel->setGeometry(250, 5, 180, 180);
    InfoLabel->setStyleSheet("QLabel{font:18px;}");
    InfoLabel->setText("副词条信息");

    CreateDB();
    ReadId();



}



//窗口析构函数
MainWindow::~MainWindow()
{
    DeleDB();
    delete ui;
}



//删除记录的列表交互
void MainWindow::DeleWithIDList()
{
    if(this->InfoList->count()!=0)
    {
        int CrrntLn = this->InfoList->currentRow();
        QString PrcssStr= IdList.at(CrrntLn);
        //Dele(PrcssStr);
        DelBttn->Dele(PrcssStr);
        IdList.removeAt(CrrntLn);
        if(CrrntLn==IdList.size())
            CrrntLn--;
        RenewList(CrrntLn);
        //使得删除后信息会随时更新

        if(this->InfoList->count()>0)
        {
            int Crrntl=this->InfoList->currentRow();
            emit InfoList->itemClicked(this->InfoList->item(Crrntl));
        }

    }else{
        QMessageBox::information(this,"警告","请先更新或者生成记录！");
    }
}


//生成记录的列表交互
void MainWindow::GenWithIDList()
{
    //IdList.append(GenerateInit());
    IdList.append(GenBttn->GenerateInit());
    RenewList();
    ListInit(IdList.count()-1);
    int Crrntl=this->InfoList->currentRow();
    emit InfoList->itemClicked(this->InfoList->item(Crrntl));
}


//刷新记录的列表交互
void MainWindow::RenewListSlot()
{
    RenewList();
}

//升级记录的列表交互
void MainWindow::UpGWithIDList()
{
    if(this->InfoList->count()!=0)
    {
        QString ID=IdList.at(this->InfoList->currentRow());
        UpGBttn->Upgrade(ID);
        //Upgrade(ID);
        int Crrntl=this->InfoList->currentRow();

        RenewList(Crrntl);
        emit InfoList->itemClicked(this->InfoList->item(Crrntl));
    }else{
        QMessageBox::information(this,"警告","请先更新或者生成记录！");
    }
}

//可变的副词条信息显视
void MainWindow::ListItemClicked()
{
    QSqlQuery Query;
    QString Sub1,Sub2,Sub3,Sub4;
    float Val1,Val2,Val3,Val4;
    QString Select=QString("select 圣遗物副词条1, 圣遗物副词条2, 圣遗物副词条3,圣遗物副词条4,副词条数值1, 副词条数值2, 副词条数值3,副词条数值4,等级 from List where 标识码 = '%1'").arg(IdList.at(this->InfoList->currentRow()));
    QString Output;
    Query.exec(Select);
    Query.next();
    Sub1=" "+Query.value(0).toString()+" ";
    Sub2=" "+Query.value(1).toString()+" ";
    Sub3=" "+Query.value(2).toString()+" ";
    Sub4=" "+Query.value(3).toString()+" ";
    Val1=Query.value(4).toFloat();
    Val2=Query.value(5).toFloat();
    Val3=Query.value(6).toFloat();
    Val4=Query.value(7).toFloat();
    //最大等级为20级
    if(Query.value(8).toUInt()==20)
    {

        this->UpGBttn->setEnabled(false);
    }else
    {

        this->UpGBttn->setEnabled(true);
    }
    if(Val4!=0)
    {
        Output=Sub1+QString::number(Val1,'f',1)+"\n"+Sub2+QString::number(Val2,'f',1)+"\n"+Sub3+QString::number(Val3,'f',1)+"\n"+Sub4+QString::number(Val4,'f',1);
    }else
    {
        Output=Sub1+QString::number(Val1,'f',1)+"\n"+Sub2+QString::number(Val2,'f',1)+"\n"+Sub3+QString::number(Val3,'f',1);
    }

    this->InfoLabel->setText(Output);
}










